package win.smartown.android.library.certificateCamera;

import android.graphics.Bitmap;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import okio.BufferedSink;

public class OkHttpUpUtil {
    private static final String TAG = "OkHttpUpUtil";
    private String mUpUrl;
    private File mPath;
    private Call mCall;
    private Map<String, String> mParams;
    private long mAlreadyUpLength = 0;//已经上传长度
    private long mTotalLength = 0;//整体文件大小
    private int mSign = 0;
    private HttpUpListener mHttpUpListener;


    private class HttpUpListener {
    }

    /**
     * 上传图片
     *
     * @param url
     * @param imagePath 图片路径
     * @return 新图片的路径
     * @throws IOException
     * @throws JSONException
     */
    public String uploadImage(String url, String imagePath) throws IOException, JSONException {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30,TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();

        Log.d("imagePath", imagePath);
        File file = new File(imagePath);

        RequestBody image = RequestBody.create(MediaType.parse("image/jpeg"), file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("photo", imagePath, image)

                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = okHttpClient.newCall(request).execute();

        return response.body().string();
    }


    public String uploadImage(String url, String imagePath,String idcard) throws IOException, JSONException {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30,TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();

        Log.d("imagePath", imagePath);
        File file = new File(imagePath);

        RequestBody image = RequestBody.create(MediaType.parse("image/jpeg"), file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("photo", imagePath, image)
                .addFormDataPart("id",idcard)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        Response response = okHttpClient.newCall(request).execute();

        return response.body().string();
    }


    public Call uploadImage1(String url, String imagePath,String idcard) throws IOException, JSONException {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30,TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();

        Log.d("imagePath", imagePath);
        File file = new File(imagePath);

        RequestBody image = RequestBody.create(MediaType.parse("image/jpeg"), file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("photo", imagePath, image)
                .addFormDataPart("id",idcard)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Connection", "keep-alive")
                .post(requestBody)
                .build();

        Call response = okHttpClient.newCall(request);
        return response;
    }


    public Call uploadImage1(String url, List<File> files, String idcard) throws IOException, JSONException {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30,TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();

        RequestBody image1 = RequestBody.create(MediaType.parse("image/jpeg"), files.get(0));
        RequestBody image2 = RequestBody.create(MediaType.parse("image/jpeg"), files.get(1));
        RequestBody image3 = RequestBody.create(MediaType.parse("image/jpeg"), files.get(2));

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("photo1", files.get(0).getName(), image1)
                .addFormDataPart("photo2", files.get(1).getName(), image2)
                .addFormDataPart("photo3", files.get(2).getName(), image3)
                .addFormDataPart("id",idcard)
                .build();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Connection", "keep-alive")
                .post(requestBody)
                .build();

        Call response = okHttpClient.newCall(request);
        return response;
    }

}

